/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.manager.editairport;

import assignment_cp3120.dataObjects.DepartureLocation;
import assignment_cp3120.ucm.UC2AL;
import assignment_cp3120.ucm.UCEditAirports;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Administrator
 */
public class EditAirportView extends JPanel {
    private EditAirportModel model;
            
    private JLabel airportNameLabel;
    private JPanel airportNamePanel;
    private JTextField airportNameField;
    
    private JLabel airportCodeLabel;
    private JPanel airportCodePanel;
    private JTextField airportCodeField;    
    
    private JLabel airportTaxLabel;
    private JPanel airportTaxPanel;
    private JTextField airportTaxField;
    
    private JButton editButton;
    
    private JComboBox editAirportChoiceCBox;

    public EditAirportView(EditAirportModel model) {
        this.model = model;
        createView();
    }
    
    public EditAirportView() {
        this(new EditAirportModel());
    }
    private void createView() {
        FlowLayout fl = new FlowLayout(FlowLayout.LEFT);

        editButton = new JButton("Edit");
        editButton.addActionListener(new UC2AL(new UCEditAirports(this,model)));
        
        airportNameLabel = new JLabel("Change the name of the airport:");
        airportNamePanel = new JPanel(fl);
        airportNameField = new JTextField("mmmmmmmmmmmmmmmmmmmmmmmm");
        airportNameField.setPreferredSize(airportNameField.getPreferredSize());
        airportNameField.setText("");

        airportCodeLabel = new JLabel("Change the alpha-numeric code:");
        airportCodePanel = new JPanel(fl);
        airportCodeField = new JTextField("mmm");
        airportCodeField.setPreferredSize(airportCodeField.getPreferredSize());
        airportCodeField.setText("");

        airportTaxLabel = new JLabel("Change the airports Tax:   ");
        airportTaxPanel = new JPanel(fl);
        airportTaxField = new JTextField("mmm");
        airportTaxField.setPreferredSize(airportTaxField.getPreferredSize());
        airportTaxField.setText("0");

        editAirportChoiceCBox = new JComboBox(model.getAirports());
        editAirportChoiceCBox.addItemListener(new EditCBoxChange());
        setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        System.out.println("");
        airportNamePanel.add(airportNameLabel);
        airportNamePanel.add(airportNameField);
        
        airportCodePanel.add(airportCodeLabel);
        airportCodePanel.add(airportCodeField);
        
        airportTaxPanel.add(airportTaxLabel);
        airportTaxPanel.add(airportTaxField);
        add(editAirportChoiceCBox);
        add(airportCodePanel);
        add(airportNamePanel);
        add(airportTaxPanel);
        add(editButton);        
    }
    public String getId(){
        return airportCodeField.getText();
    }
    public String getAirportName(){
        return airportNameField.getText();
    }
    public int getAirportTax(){
        return Integer.parseInt(airportTaxField.getText());
    }
    public DepartureLocation getEditAirport(){
        return model.getAirports().get(editAirportChoiceCBox.getSelectedIndex());
    }
    public void clearView() {
        model.loadAirports();
        this.removeAll();
        createView();
        this.validate();
    }
    private class EditCBoxChange implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            DepartureLocation tempAirport= model.getAirports().get(editAirportChoiceCBox.getSelectedIndex());

            airportCodeField.setText(tempAirport.getDep_id());
            airportNameField.setText(tempAirport.getDep_name());
            airportTaxField.setText(Integer.toString(tempAirport.getAirport_tax()));
        }
    }
}
